package org.opsli.modulars.business.iot.coder;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import org.opsli.modulars.business.app.web.ZkUtils;
import org.opsli.modulars.business.iot.protocol.Constans;
import org.opsli.modulars.business.iot.protocol.HexDump;
import org.opsli.modulars.business.iot.protocol.TCPProtocol;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author sst
 * @version V1.0
 * @Package com.lysoft.monitoring.iot.coder
 * @date 2019-08-22 14:23
 * 编码器
 */
public class EncoderHandler extends MessageToByteEncoder<String> {

    private static Logger logger = LoggerFactory.getLogger(EncoderHandler.class);

    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, String s, ByteBuf byteBuf) throws Exception {
        //将16进制字符串转为数组
        /*byteBuf.writeBytes(hexString2Bytes(s));*/
    }


    /**

     * @Title:hexString2Bytes

     * @Description:16进制字符串转字节数组

     * @param src 16进制字符串

     * @return 字节数组

     */

    public static byte[] hexString2Bytes(String src) {

        int l = src.length() / 2;

        byte[] ret = new byte[l];

        for (int i = 0; i < l; i++) {

            ret[i] = (byte) Integer.valueOf(src.substring(i * 2, i * 2 + 2), 16).byteValue();

        }
        return ret;
    }


    /*@Override
    protected void encode(ChannelHandlerContext ctx,String msg, ByteBuf out) throws Exception {
        if(msg instanceof TCPProtocol){
            TCPProtocol tcpProtocol = (TCPProtocol)msg;
            devEncode(ctx,tcpProtocol,out);
        }else{
            // 要发送的数据
            // 这里如果是自定义的类型，msg即为自定义的类型，需要转为byte[]
            byte[] body = ((ByteBuf)msg).array();

            // 数据长度
            int dataLength = body.length;
            // 缓冲区先写入数据长度
            out.writeInt(dataLength);
            // 再写入数据
            out.writeBytes(body);
        }

    }*/


    public void devEncode(ChannelHandlerContext ctx, TCPProtocol tcpProtocol, ByteBuf out){
        byte[] bytes=new byte[18+tcpProtocol.getData().length-1];
        out.writeByte(tcpProtocol.getHead_data());
        out.writeShort(tcpProtocol.getHeadLen());
        bytes[0]=0x00;
        bytes[1]=(byte)tcpProtocol.getHeadLen();
        int addLength = tcpProtocol.getAdderss().length;
        for(int i=0;i<addLength;i++){
            bytes[2+i]=tcpProtocol.getAdderss()[i];
        }
        out.writeBytes(tcpProtocol.getAdderss());
        out.writeByte(tcpProtocol.getIdentify());
        bytes[8]=tcpProtocol.getIdentify();

        out.writeByte(tcpProtocol.getAfn());
        bytes[9]=tcpProtocol.getAfn();
        out.writeByte(tcpProtocol.getContentLength());
        bytes[10]=tcpProtocol.getContentLength();

        byte[] datas = tcpProtocol.getData();
        System.arraycopy(datas,0,bytes,11,datas.length);
        out.writeBytes(datas);
        String currentTime = ZkUtils.getDateStr();
        String[] arr = currentTime.split("-");
        byte[] dates =new byte[arr.length];
        for(int i=0;i<arr.length;i++){
            dates[i] = (byte)Integer.parseInt(arr[i]);
            bytes[11+datas.length+i]=dates[i];
        }
        System.out.println("aaa::"+ HexDump.toHexString(bytes));
        out.writeBytes(dates);
        byte cs = Constans.Check_CS(bytes);
        System.out.println("cs::"+HexDump.toHexString(cs));
        out.writeByte(cs);
        out.writeByte(tcpProtocol.getTail());
    }



}
